package oa.hleast.xswl.controller.admin.activity;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.xswl.pojo.domain.activity.CmsActivityAttributes;
import oa.hleast.xswl.pojo.domain.activity.CmsActivityType;
import oa.hleast.xswl.service.activity.ICmsActivityAttributesService;
import oa.hleast.xswl.service.activity.ICmsActivityTypeService;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

@Api(tags = "【系统管理】 活动报名类型")
@RestController("ActivityTypeController")
@RequestMapping("/api.admin/v1/activity/type")
@Slf4j
@AllArgsConstructor
public class ActivityTypeController {

    private final ICmsActivityTypeService activityTypeServiceService;
    private final ICmsActivityAttributesService attributesService;

    @ApiOperation(value = "类型列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    })
    @GetMapping("/pageList")
    public Result list(@RequestParam @NotNull String queryMode, @RequestParam @NotNull Integer page, @RequestParam @NotNull Integer limit) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:
                Page<CmsActivityType> result = activityTypeServiceService.pageList(page, limit, null, null);
                return Result.success(result.getRecords(), result.getTotal());
        }
    }

    @ApiOperation(value = "活动报名类型详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "活动报名类型id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("detail/{id}")
    public Result detail(@PathVariable @NotNull Long id) {
        CmsActivityType vo = activityTypeServiceService.detail(id);
        return Result.success(vo);
    }

    @ApiOperation(value = "新增活动报名类型", httpMethod = "POST")
    @ApiImplicitParam(name = "form", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsActivityType",dataTypeClass = CmsActivityType.class)
    @PostMapping("/add")
    public Result add(@RequestBody CmsActivityType form) {
        Boolean result = activityTypeServiceService.add(form);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改活动报名类型", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动报名类型id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "form", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsActivityType",dataTypeClass = CmsActivityType.class)
    })
    @PutMapping(value = "/update/{id}")
    public Result update(
            @PathVariable @NotNull Integer id,
            @RequestBody @NotNull CmsActivityType form) {

        Boolean result = activityTypeServiceService.update(form);
        return Result.judge(result);
    }

    @ApiOperation(value = "删除活动报名类型", httpMethod = "DELETE")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") String ids) {

        Boolean result = activityTypeServiceService.deleteByIds(Arrays.asList(ids.split(",")));
        return Result.judge(result);
    }

    @ApiOperation(value = "属性列表", httpMethod = "GET")
    @ApiImplicitParam(name = "typeId", value = "活动类型ID", paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/attribute/pageList")
    public Result attributeList(@RequestParam @NotNull Long typeId) {
        List<CmsActivityAttributes> list = attributesService
                .list(new LambdaQueryWrapper<CmsActivityAttributes>()
                        .eq(CmsActivityAttributes::getActivityTypeId, typeId));
        return Result.success(list);
    }

    @ApiOperation(value = "属性详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "活动报名类型id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/attribute/detail/{id}")
    public Result attributeDetail(@PathVariable @NotNull Long id) {
        CmsActivityAttributes vo = attributesService.detail(id);
        return Result.success(vo);
    }

    @ApiOperation(value = "新增属性", httpMethod = "POST")
    @ApiImplicitParam(name = "formList", value = "实体JSON对象", required = true, allowMultiple = true, paramType = "body", dataType = "CmsActivityAttributes",dataTypeClass = CmsActivityAttributes.class)
    @PostMapping("/attribute/add")
    public Result attributeAdd(@RequestBody List<CmsActivityAttributes> formList) {
        Boolean result = attributesService.attributeAdd(formList);
        return Result.success(result);
    }

    @ApiOperation(value = "修改属性", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动报名类型id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "form", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsActivityAttributes",dataTypeClass = CmsActivityAttributes.class)
    })
    @PutMapping(value = "/attribute/update/{id}")
    public Result attributeUpdate(
            @PathVariable @NotNull Integer id,
            @RequestBody @NotNull CmsActivityAttributes form) {

        Boolean result = attributesService.update(form);
        return Result.success(result);
    }

    @ApiOperation(value = "删除属性", httpMethod = "DELETE")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @DeleteMapping("/attribute/{ids}")
    public Result attributeDelete(@PathVariable("ids") String ids) {

        Boolean result = activityTypeServiceService.deleteByIds(Arrays.asList(ids.split(",")));
        return Result.success(result);
    }
}
